﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RFPL
{
    public partial class StadionInfo : Form
    {
        public int stadionId = 0;
        private int fk_city = 0;

        public string stadionName = "";
        public string cityName = "";

        public StadionInfo(int id)
        {
            InitializeComponent();
            stadionId = id;
        }

        private void StadionInfo_Load(object sender, EventArgs e)
        {
            UpdateClubList();
            
            if (stadionId != 0) {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                conn.Open();
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[getStadionInfo]";
                myCommand.Parameters.Add("@id", SqlDbType.Int).Value = stadionId;
                SqlDataReader dataReader = myCommand.ExecuteReader();
                if (dataReader.Read())
                {
                    tbStadionName.Text = Convert.ToString(dataReader[0]);

                    if (!DBNull.Value.Equals(dataReader[1]))
                    {
                        fk_city = Convert.ToInt32(dataReader[1]);

                        foreach (DataGridViewRow row in dgvCities.Rows) // select city of stadion in dgv
                        {
                            if (Convert.ToInt32(row.Cells["id"].Value) == fk_city)
                            {
                                row.Selected = true;
                                break;
                            }
                        }
                    }

                }
                dataReader.Close();
                conn.Close();
            }
        }

        private void UpdateClubList()
        {

            dgvCities.Rows.Clear();

            SqlDataReader dataReader = Global.getDataReader("[getAllCities]");
            while (dataReader.Read())
            {
                dgvCities.Rows.Add(dataReader["id"], dataReader["name"]);

            }
            dataReader.Close();
            Global.closeConnection();
        }

        private void setSaveState() {

            if (dgvCities.SelectedRows.Count > 0 && tbStadionName.Text.Length > 0)
            {
                save.Enabled = true;
            }
            else {
                save.Enabled = false;
            }

        }

        private void tbStadionName_TextChanged(object sender, EventArgs e)
        {
            setSaveState();
        }

        private void dgvCities_SelectionChanged(object sender, EventArgs e)
        {
            setSaveState();

            if (dgvCities.SelectedRows.Count > 0) {
                editCity.Enabled = true;
                delCity.Enabled = true;
            }
            else
            {
                editCity.Enabled = false;
                delCity.Enabled = false;
            }
        }

        private void save_Click(object sender, EventArgs e)
        {
            fk_city = Convert.ToInt32(dgvCities.SelectedRows[0].Cells["id"].Value);

            if (stadionId == 0)
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[addStadion]";
                myCommand.Parameters.Add("@name", SqlDbType.NVarChar, tbStadionName.Text.Length).Value = Convert.ToString(tbStadionName.Text);
                myCommand.Parameters.Add("@fk_city", SqlDbType.Int).Value = fk_city;

                conn.Open();
                stadionId = Convert.ToInt32(myCommand.ExecuteScalar());
                conn.Close();
            }
            else
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[editStadion]";
                myCommand.Parameters.Add("@id", SqlDbType.Int).Value = stadionId;
                myCommand.Parameters.Add("@name", SqlDbType.NVarChar, tbStadionName.Text.Length).Value = Convert.ToString(tbStadionName.Text);
                myCommand.Parameters.Add("@fk_city", SqlDbType.Int).Value = fk_city;
                               
                conn.Open();
                myCommand.ExecuteScalar();
                conn.Close();
            }

            this.Close();
        }

        private void newCity_Click(object sender, EventArgs e)
        {
            CityInfo ci = new CityInfo(0);
            ci.ShowDialog();

            UpdateClubList();
        }

        private void editCity_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(dgvCities.SelectedRows[0].Cells["id"].Value);
            CityInfo ci = new CityInfo(id);
            ci.ShowDialog();

            UpdateClubList();
        }

        private void del_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(dgvCities.SelectedRows[0].Cells["id"].Value);

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[delStadion]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = id;

            conn.Open();
            myCommand.ExecuteScalar();
            conn.Close();

            UpdateClubList();
        }

        private void StadionInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (stadionId != 0) {
                stadionName = tbStadionName.Text;
                cityName = Convert.ToString(dgvCities.SelectedRows[0].Cells["name"].Value);
            }
        }
    }
}
